package com.zhang.servicemap.remote;

import com.zhang.internalcommon.constant.AmapConfigConstant;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.internalcommon.response.ServiceResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 类功能描述
 *
 * @author Kye
 * @date 2025-04-15
 */
@Service
@Slf4j
public class ServiceClient {
	private static final Logger log = LoggerFactory.getLogger(ServiceClient.class);
	@Value("${amap.key}")
	private String myKey;

	@Autowired
	private RestTemplate restTemplate;

	public ResponseResult add(String name) {
		// 拼装请求的url
		StringBuilder url = new StringBuilder();
		url.append(AmapConfigConstant.SERVICE_ADD_URL);
		url.append("?");
		url.append("key=" + myKey);
		url.append("&");
		url.append("name=" + name);

		ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null, String.class);
		String body = forEntity.getBody();
		JSONObject result = JSONObject.fromObject(body);
		JSONObject data = result.getJSONObject("data");
		String sid = data.getString("sid");
		ServiceResponse serviceResponse = new ServiceResponse();
		serviceResponse.setSid(sid);
		return ResponseResult.success(serviceResponse);
	}
}
